package DBtools;

import java.util.HashMap;
import java.util.List;

import tools.SysAlert;
import model.UserModel;
import model.transfermodel.BeatPackage;

public class UserBO extends BaseDao {
	public static int CREATE_SUCCESS = 0;
	public static int DOUBLE_USER = 1;
	
	
	private String SQLInsert = "INSERT INTO user(user,pwd,userlevel,last_login_time,last_login_ip) VALUES (?, ?, ?, ?, ?)";
	private String SQLQueryALL   = "Select * from user";
	private String SQLQuery   = "Select * from user Where user = ?";
	private String SQLUpdate = "UPDATE user SET  pwd = ?, userlevel = ?  WHERE user = ? ";
	private String SQLUpdateLog = "UPDATE user SET last_login_time =?, last_login_ip = ? WHERE user = ?";
	
	public List queryall(){
		Object[] objects = new Object[0];
		int[] types   = new int[0];
		
		List list = executeQuery(SQLQueryALL, objects, types);
		return list;
	}
	
	public int createUser(UserModel model){
		List list = find(model.user);
		if(list.size() != 0){
			return DOUBLE_USER;
		}
		
		Object[] objects = new Object[5];
		int[] types   = new int[5];
		
		objects[0] = model.user;
		types[0] = java.sql.Types.VARCHAR;
		
		objects[1] = model.pwd;
		types[1] = java.sql.Types.VARCHAR;
		
		objects[2] = model.userlevel;
		types[2] = java.sql.Types.INTEGER;
		
		objects[3] = model.lastLoginTime;
		types[3] = java.sql.Types.VARCHAR;
		
		objects[4] = model.LastLoginIp;
		types[4] = java.sql.Types.VARCHAR;
		
		executeUpdate(SQLInsert, objects, types);
		return CREATE_SUCCESS;
	}
	
	public List find(String user){
		BaseDao baseDao = new BaseDao();
		Object[] objects = new Object[1];
		int[]    types   = new int[1];
		
		objects[0] = user;
		types[0]   = java.sql.Types.VARCHAR;
		
		List list = baseDao.executeQuery(SQLQuery, objects, types);
		return list;
	}
	
	public boolean updateLoginInfo(UserModel model){
		List list = find(model.user);
		if(list.size() == 0){
			return false;
		}
		
		Object[] objects = new Object[3];
		int[] types   = new int[3];
		
		objects[0] = model.lastLoginTime;
		types[0] = java.sql.Types.VARCHAR;
		
		objects[1] = model.LastLoginIp;
		types[1] = java.sql.Types.VARCHAR;
		
		objects[2] = model.user;
		types[2] = java.sql.Types.VARCHAR;
		
		executeUpdate(SQLUpdateLog, objects, types);
		
		return true;
		
	}
	
	public boolean updateUser(UserModel model){
		List list = find(model.user);
		if(list.size() == 0){
			return false;
		}
		
		Object[] objects = new Object[3];
		int[] types   = new int[3];
		
		
		
		objects[0] = model.pwd;
		types[0] = java.sql.Types.VARCHAR;
		
		objects[1] = model.userlevel;
		types[1] = java.sql.Types.INTEGER;
		
		objects[2] = model.user;
		types[2] = java.sql.Types.VARCHAR;
		
		executeUpdate(SQLUpdate, objects, types);
		
		return true;
	}
	
	public  static void main(String[] args ){
		UserModel model = new UserModel();
		model.lastLoginTime = "2015-7-10 14:00:00";
		model.LastLoginIp = "110.64.91.235";
		model.userlevel = 1;
		UserBO bo = new UserBO();
		SysAlert.info("UserBO", bo.updateLoginInfo(model)+"");
	}
}
